# -*- coding: utf-8 -*-
from django.shortcuts import render_to_response
from django.template.context import RequestContext
from main.models import Rest, Rest_dish
import string


def index(request, template_name='index.html'):
    rests = Rest.objects.all().order_by("-weight")[0:6]
    rest_dishs = Rest_dish.objects.all().order_by("-weight")[0:5]
    return render_to_response(template_name, {'rests':rests,'rest_dishs':rest_dishs}, context_instance=RequestContext(request))

def order(request, template_name='order.html'):
    manager = Rest.objects
    querySet  = manager.get_query_set()
    selectField = {}
    orderField = {}
    querySet = querySet.filter()
    
    if request.GET.get('busarea'):
        selectField['busarea'] = string.atoi(request.GET['busarea'])
        querySet = querySet.filter(bus_area=request.GET['busarea'])
    if request.GET.get('cuisine'):
        selectField['cuisine'] = string.atoi(request.GET['cuisine'])
        querySet = querySet.filter(cuisine=request.GET['cuisine'])
    if request.GET.get('spend'):
        selectField['spend'] = string.atoi(request.GET['spend'])
        querySet = querySet.filter(spend=request.GET['spend'])
        
        
    querySet = querySet.order_by("-weight")
    if request.GET.get('orderspend'):
        orderField['orderspend'] = request.GET['orderspend']
        querySet = querySet.order_by(request.GET.get('orderspend'))    
        
    rest_dishs = Rest_dish.objects.all().order_by("-weight")[0:5]  
    return render_to_response(template_name,{"rests":querySet,'selectField':selectField,'orderField':orderField,'rest_dishs':rest_dishs},  context_instance=RequestContext(request))

def show_rest(request, alias, template_name='rest.html'):
    rest = Rest.objects.get(alias__exact=alias)
    return render_to_response(template_name,{'rest':rest}, context_instance=RequestContext(request))

def show_dish(request, alias, template_name='rest_dish.html'):
    rest = Rest.objects.get(alias__exact=alias)
    dishs = Rest_dish.objects.filter(rest__alias__exact=alias)
    return render_to_response(template_name,{'dishs':dishs,'rest':rest}, context_instance=RequestContext(request))

def show_spec_dish(request, alias, template_name='rest_spec_dish.html'):
    rest = Rest.objects.get(alias__exact=alias)
    specdishs = Rest_dish.objects.filter(rest__alias__exact=alias,special =True)
    return render_to_response(template_name,{'specdishs':specdishs,'rest':rest}, context_instance=RequestContext(request))

def show_traffic(request, alias,template_name='rest_traffic.html'):
    rest = Rest.objects.get(alias__exact=alias)
    return render_to_response(template_name, {'rest':rest}, context_instance=RequestContext(request))

def show_comment_dish(request, alias, template_name='rest_comment_dish.html'):
    rest = Rest.objects.get(alias__exact=alias)
    return render_to_response(template_name,{'rest':rest},context_instance=RequestContext(request))


def test(request, template_name='test.html'):
    return render_to_response(template_name,  context_instance=RequestContext(request))

    